package com.app.mvvmlearnmodule.model

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.paging.DataSource
import androidx.paging.PagedList
import androidx.paging.toLiveData
import com.app.mvvmlearnmodule.entity.Entity01
import com.app.mvvmlearnmodule.entity.Entity02

class MVVMLearnModel : ViewModel() {
    val TAG = MVVMLearnModel::class.java.simpleName
    val mutableEntity01LiveData = MutableLiveData<Entity01>().also {
        it.value = Entity01()
    }

    val mutableEntity02LiveData = MutableLiveData<MutableList<Entity02>>().also {
        it.value = mutableListOf()
        for (index in 10..50) {
            val entity02 = Entity02()
            entity02.name01 = "111111"
            entity02.name02 = "切换"
            it.value?.add(entity02)
        }
    }


    inner class DataSourceFactory :  DataSource.Factory<Int, Entity02>() {
        override fun create(): DataSource<Int, Entity02> {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }

    val entity02LiveData: LiveData<PagedList<Entity02>> = DataSourceFactory().toLiveData(pageSize = 50)


    fun updateMutableEntity01() {

        val entity01 = mutableEntity01LiveData.value
        Log.d(TAG, "updateMutableEntity01 -> entity01 -> $entity01")
        entity01?.btnName01 = if (entity01?.btnName01.equals("zhouwd")) {
            "kengbijia"
        } else {
            "zhouwd"
        }

    }


}